import pymysql
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QMainWindow

from main_win.setdata import Ui_setdatawin
from utils.CustomMessageBox import MessageBox


class setdata_window(QMainWindow, Ui_setdatawin):
    # connect to localhost mysql
    mysql = pymysql.connect(
        host='127.0.0.1',
        user='root',
        password='123456',
        port=3306,
        db='imagerecognition',
        autocommit=True,
    )
    cursor = mysql.cursor()
    # mysql operate statements
    sql = "SELECT * FROM users WHERE username=%s"
    insql = "INSERT INTO users(username,password) values(%s,%s)"
    upsql = "UPDATE users SET username=%s WHERE id=%s"
    upsql2 = "UPDATE users SET password=%s WHERE id=%s"
    send = pyqtSignal()
    # user data changed signal
    send_change = pyqtSignal()

    def __init__(self, parent=None):
        super(setdata_window, self).__init__(parent)
        self.setupUi(self)
        self.m_flag = False
        # user id according to the main window
        self.userid = 1
        # no border and transparent
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowFlags(Qt.FramelessWindowHint)

        self.closeButton.clicked.connect(self.close)
        self.minButton.clicked.connect(self.showMinimized)
        # checked and renew user data
        self.pushButton.clicked.connect(self.check)
        self.backbutton.clicked.connect(self.hide)

    def check(self):
        name = self.name.text()
        p1 = self.password.text()
        p2 = self.password2.text()
        # user want to change the name
        if name != "":
            # search the new name in mysql
            self.cursor.execute(self.sql, name)
            a = self.cursor.fetchone()
            # name exist
            if a is not None:
                MessageBox(
                    self.closeButton, title='Tips', text='Account Existed!', time=1500, auto=True).exec_()
                # clear the line edit for user to input again
                self.name.clear()
            # name not exist
            else:
                # renew the username in mysal according to id
                self.cursor.execute(self.upsql, (name, self.userid))
                MessageBox(
                    self.closeButton, title='Tips', text='Set Name Successfully!', time=1500, auto=True).exec_()
                # refresh userdata on main window and userdata window
                self.send_change.emit()
        # user want to change password
        if p1 != "" and p2 != "":
            # clear the line edit for user to input again
            self.password.clear()
            self.password2.cursor()
            # passwords inconsistent
            if p1 != p2:
                MessageBox(
                    self.closeButton, title='Tips', text='Passwords Inconsistent!', time=1500, auto=True).exec_()
            # passwords consists
            else:
                # renew the user password inmysql according to user id
                self.cursor.execute(self.upsql2, (p1, self.userid))
                MessageBox(
                    self.closeButton, title='Tips', text='Set Password Successfully!', time=1500, auto=True).exec_()

    # rewire events bout mouse to realize window moving
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.m_flag = True
            self.m_Position = event.globalPos() - self.pos()
            event.accept()

    def mouseMoveEvent(self, QMouseEvent):
        if Qt.LeftButton and self.m_flag:
            self.move(QMouseEvent.globalPos() - self.m_Position)
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):
        self.m_flag = False
        self.setCursor(QCursor(Qt.ArrowCursor))
